package cn.tedu.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
//可以使当前实体类在返回前端的时候忽略字段属性为null的字段，使其为null字段不显示
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> {
    //状态码
    private Integer code;
    //提示信息，如果有错误时，前端可以获取该字段进行提示
    private String msg;
    //查询到的结果数据，
    private T data;

    public ResponseResult(Integer statusCode, String message) {
        this.code = statusCode;
        this.msg = message;
    }

    public ResponseResult(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.msg = statusCode.getMsg();
    }

    public ResponseResult(StatusCode statusCode, String message) {
        this.code = statusCode.getCode();
        this.msg = statusCode.getMsg();
        this.data = (T) message;
    }


    /**
     * 正常返回 入参 有数据和消息
     *
     * @param data
     * @param msg
     * @return
     */

    public static ResponseResult ok(Object data, String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(data);
        responseResult.setCode(2000);//OK
        responseResult.setMsg(msg);
        return responseResult;
    }

    /**
     * 正常返回 入参只有数据
     *
     * @param data
     * @return
     */
    public static ResponseResult ok(Object data) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(data);
        responseResult.setCode(2000);//OK
        return responseResult;
    }

    public static ResponseResult ok(){
        return ok(null);
    }
}